#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from selenium import webdriver
from Utils.PageUtils import PageUtil
from Utils.Wait import Wait
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from time import sleep
from Utils.Logger import Logger
from Config.GlobalVariables import WAIT_UNTIL_TIMEOUT, WAIT_FREQUENCY


class Element(object):

    def __init__(self, _driver, locator):
        self.locator = locator
        self.driver = _driver
        self.logger = Logger().get_logger()

    def elements(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            self.logger.debug("Wait element visible")
            Wait(self.driver).element_visible(self.locator, timeout, frequency)
            self.logger.debug("Get elements")
            elements = PageUtil(self.driver).get_elements(self.locator, timeout, frequency)
            return elements
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return None
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def click(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            self.logger.debug("Wait element clickable")
            Wait(self.driver).element_clickable(self.locator, timeout, frequency)
            self.logger.debug("Get element and click")
            PageUtil(self.driver).get_element(self.locator, timeout, frequency).click()
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def clear(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            self.logger.debug("Wait element clickable")
            Wait(self.driver).element_clickable(self.locator, timeout, frequency)
            self.logger.debug("Get element and clear")
            PageUtil(self.driver).get_element(self.locator, timeout, frequency).clear()
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def send_keys(self, value, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            self.logger.debug("Wait element clickable")
            Wait(self.driver).element_clickable(self.locator, timeout, frequency)
            self.logger.debug("Get element and send keys")
            PageUtil(self.driver).get_element(self.locator, timeout, frequency).send_keys(value)
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def submit(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            self.logger.debug("Wait element clickable")
            Wait(self.driver).element_clickable(self.locator, timeout, frequency)
            self.logger.debug("Get element and send submit")
            PageUtil(self.driver).get_element(self.locator, timeout, frequency).submit()
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def get_text(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            self.logger.debug("Wait element visible")
            Wait(self.driver).element_visible(self.locator, timeout, frequency)
            self.logger.debug("Get element and get its text")
            text = PageUtil(self.driver).get_element(self.locator, timeout, frequency).text
            return text
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def get_size(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            Wait(self.driver).element_clickable(self.locator, timeout, frequency)
            size = PageUtil(self.driver).get_element(self.locator, timeout, frequency).size
            return size
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def get_attribute(self, attribute, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            Wait(self.driver).element_present(self.locator, timeout, frequency)
            value = PageUtil(self.driver).get_element(self.locator, timeout, frequency).get_attribute(attribute)
            return value
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def is_displayed(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            Wait(self.driver).element_visible(self.locator, timeout, frequency)
            result = PageUtil(self.driver).get_element(self.locator, timeout, frequency).is_displayed()
            return result
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def is_enabled(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            Wait(self.driver).element_visible(self.locator, timeout, frequency)
            result = PageUtil(self.driver).get_element(self.locator, timeout, frequency).is_enabled()
            return result
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def is_selected(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            Wait(self.driver).element_visible(self.locator, timeout, frequency)
            result = PageUtil(self.driver).get_element(self.locator, timeout, frequency).is_selected()
            return result
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

    def switch_to_frame(self, timeout=WAIT_UNTIL_TIMEOUT, frequency=WAIT_FREQUENCY):
        try:
            result = Wait(self.driver).frame_switchable(self.locator, timeout, frequency)
            sleep(2)  # Chrome issue, have to wait sometime after switching frame
            return result
        except (NoSuchElementException, TimeoutException):
            self.logger.warning("Cannot find this element: {0}".format(self.locator))
            return False
        except Exception:
            self.logger.exception("Exception: ", exc_info=True)
            raise

if __name__ == "__main__":
    driver = webdriver.Chrome(r"C:\Users\xiaoyao\Downloads\chromedriver.exe")
    driver.get("http://mail.126.com")

    login_frame_tab = Element(driver, ("id", "lbNormal"))
    login_frame_tab.click()
    print(login_frame_tab.get_text())
    driver.quit()
